package source

import (
	"gin-myboot/global"
	system "gin-myboot/modules/system/model"
	"github.com/gookit/color"
	"gorm.io/gorm"
	"strings"
)

var Permission = new(permission)

type permission struct{}

//@description: sys_permissions 表数据初始化
func (a *permission) Init() error {
	return global.GormDB.Transaction(func(tx *gorm.DB) error {
		if tx.Where("id IN ?", []int{1, 2}).Find(&[]system.SysPermission{}).RowsAffected == 2 {
			color.Danger.Println("\n[Mysql] --> sys_permissions 表的初始数据已存在!")
			return nil
		}

		permissionSql := `INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (1, '2021-08-22 09:34:27', '2021-09-12 14:24:55', NULL, 0, 0, '/dashboard', 'dashboard', 0, 'RouteView', 1, 0, '仪表盘', 'dashboard', 1, '/dashboard/workplace', '', 1, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (2, '2021-08-22 09:34:27', '2021-08-22 09:34:27', NULL, 0, 1, '/dashboard/workplace', 'workplace', 0, 'Workplace', 1, 0, '工作台', 'dashboard', 1, NULL, NULL, 1, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (3, '2021-08-22 09:34:27', '2021-08-22 09:34:27', NULL, 0, 1, '/dashboard/monitor', 'monitor', 0, 'modules/dashboard/views/Monitor.vue', 1, 0, '监控台', 'menu', 1, NULL, NULL, 1, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (4, '2021-08-22 09:34:27', '2021-08-22 09:34:27', NULL, 0, 1, 'https://gin-gonic.com/zh-cn/docs/', 'redirect', 0, '', 1, 0, 'gin 文档', 'menu', 1, NULL, NULL, 1, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (5, '2021-08-22 09:34:27', '2021-08-22 09:34:27', NULL, 0, 1, 'https://gorm.io/zh_CN/docs/index.html', 'gorm', 0, 'IFrameView', 1, 0, 'gorm 文档', 'menu', 1, NULL, NULL, 1, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (6, '2021-08-22 09:34:27', '2021-08-22 09:34:27', NULL, 0, 0, 'system', 'system', 0, 'RouteView', 1, 0, '系统设置', 'setting', 1, NULL, NULL, 2, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (7, '2021-08-22 09:34:27', '2021-10-24 21:33:47', NULL, 0, 6, 'permission', 'permission', 0, 'modules/system/views/permission/Index.vue', 1, 0, '权限菜单', 'menu', 1, '', '/api/v1/system/permission/getAll:POST,/api/v1/system/permission/getTree:POST,/api/v1/system/permission/delete:POST,/api/v1/system/permission/deleteByIds:POST,/api/v1/system/permission/update:POST,/api/v1/system/permission/create:POST', 2, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (8, '2021-08-22 09:34:27', '2021-10-24 21:34:30', NULL, 0, 6, 'admin', 'admin', 0, 'modules/system/views/admin/Index.vue', 1, 0, '系统管理员', 'menu', 1, '', '/api/v1/system/admin/getAll:GET,/api/v1/system/admin/delete:POST,/api/v1/system/admin/deleteByIds:POST,/api/v1/system/admin/create:POST,/api/v1/system/admin/changeStatus:POST,/api/v1/system/admin/getList:POST,/api/v1/system/admin/update:POST,/api/v1/system/admin/resetPassword:POST', 2, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (9, '2021-08-22 09:34:27', '2021-10-24 21:35:15', NULL, 0, 6, 'role', 'role', 0, 'modules/system/views/role/Index.vue', 1, 0, '系统角色', 'menu', 1, '', '/api/v1/system/role/delete:POST,/api/v1/system/role/deleteByIds:POST,/api/v1/system/role/getList:POST,/api/v1/system/role/getAll:POST,/api/v1/system/role/create:POST,/api/v1/system/role/update:POST', 2, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (10, '2021-08-22 09:34:27', '2021-10-24 21:36:04', NULL, 0, 6, 'setting', 'config', 0, 'modules/system/views/config/Index.vue', 1, 0, '系统配置', 'menu', 1, '', '/api/v1/system/config/getList:POST,/api/v1/system/config/getAll:POST,/api/v1/system/config/create:POST,/api/v1/system/config/delete:POST,/api/v1/system/config/update:POST,/api/v1/system/config/setValue:POST,/api/v1/system/config/write:POST', 2, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (11, '2021-08-22 09:34:27', '2021-10-24 21:36:53', NULL, 0, 6, 'user', 'user', 0, 'modules/system/views/user/Index.vue', 1, 0, '用户列表', 'menu', 1, '', '/api/v1/system/user/delete:POST,/api/v1/system/user/deleteByIds:POST,/api/v1/system/user/create:POST,/api/v1/system/user/changeStatus:POST,/api/v1/system/user/getList:POST,/api/v1/system/user/update:POST,/api/v1/system/user/resetPassword:POST', 2, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (14, '2021-09-15 10:01:14', '2021-10-24 21:38:05', NULL, 0, 6, 'router', 'router', 0, 'modules/system/views/api/Index.vue', 1, 0, 'API管理', 'menu', 1, '', '/api/v1/system/api/delete:POST,/api/v1/system/api/deleteByIds:POST,/api/v1/system/api/detail:POST,/api/v1/system/api/getList:POST,/api/v1/system/api/getAll:POST,/api/v1/system/api/getRoutes:POST,/api/v1/system/api/create:POST,/api/v1/system/api/update:POST', 99999, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (15, '2021-09-15 21:45:30', '2021-10-24 21:39:31', NULL, 0, 6, 'dict', 'dict', 0, 'modules/system/views/dict/Index.vue', 1, 0, '字典管理', 'menu', 1, '', '/api/v1/system/dict/detail:GET,/api/v1/system/dict/getAll:GET,/api/v1/system/dict/create:POST,/api/v1/system/dict/delete:POST,/api/v1/system/dict/update:POST,/api/v1/system/dict/getList:POST,/api/v1/system/dict/saveDetail:POST,/api/v1/system/dictDetail/create:POST,/api/v1/system/dictDetail/delete:POST,/api/v1/system/dictDetail/update:POST,/api/v1/system/dictDetail/getList:POST', 99999, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (16, '2021-09-19 16:50:54', '2021-09-19 16:53:02', NULL, 0, 0, 'storage', 'storage', 0, 'RouteView', 1, 0, '文件系统', 'folder', 1, '', '', 99999, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (17, '2021-09-19 16:52:37', '2021-10-24 22:17:24', NULL, 0, 16, 'filesystem', 'filesystem', 0, 'modules/storage/views/filesystem/Index.vue', 1, 0, '文件管理', 'menu', 1, '', '/api/v1/storage/upload/getList:POST,/api/v1/storage/upload/delete:POST,/api/v1/storage/upload/deleteByIds:POST,/api/v1/storage/upload/getUserFileList:POST,/api/v1/storage/upload/file:POST', 99999, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (18, '2021-09-21 10:13:17', '2021-10-24 22:12:49', NULL, 0, 0, 'account', 'account', 0, 'RouteView', 1, 0, '个人中心', 'menu', 1, '', '/api/v1/system/message/getUserMessages:POST,/api/v1/system/message/getUserUnreadMessages:POST', 99999, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (19, '2021-09-21 10:16:11', '2021-10-24 21:51:28', NULL, 0, 18, 'profile', 'profile', 0, 'modules/system/views/account/center/Index.vue', 1, 0, '个人资料', 'menu', 1, '', '/api/v1/common/auth/getUserInfo:POST,/api/v1/common/auth/getUserMenus:POST,/api/v1/common/auth/logout:POST,/api/v1/common/auth/changePassword:POST,/api/v1/common/auth/update:POST', 99999, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (20, '2021-09-21 10:17:37', '2021-10-24 21:54:22', NULL, 0, 18, 'settings', 'settings', 0, 'modules/system/views/account/settings/Index.vue', 1, 0, '个人设置', 'menu', 1, '', '/api/v1/common/auth/getUserInfo:POST', 99999, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (21, '2021-09-21 10:29:23', '2021-10-24 21:53:12', NULL, 0, 20, 'base', 'accountBaseSettings', 0, 'modules/system/views/account/settings/BaseSettings.vue', 1, 0, '基本资料', 'menu', 1, '', '/api/v1/common/auth/getUserInfo:POST,/api/v1/common/auth/update:POST', 99999, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (22, '2021-09-21 10:30:10', '2021-10-24 21:53:25', NULL, 0, 20, 'password', 'accountPassword', 0, 'modules/system/views/account/settings/Password.vue', 1, 0, '密码修改', 'menu', 1, '', '/api/v1/common/auth/changePassword:POST', 99999, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (23, '2021-10-08 11:31:44', '2021-10-25 11:59:02', NULL, 0, 6, 'log', 'log', 0, 'modules/system/views/log/Index.vue', 1, 0, '操作日志', 'menu', 1, '', '/api/v1/system/log/delete:POST,/api/v1/system/log/deleteByIds:POST,/api/v1/system/log/detail:POST,/api/v1/system/log/create:POST,/api/v1/system/log/getList:POST,/api/v1/system/log/deleteAll:POST', 99999, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (24, '2021-10-24 14:21:58', '2021-10-24 21:40:27', NULL, 0, 6, 'department', 'department', 0, 'modules/system/views/department/Index.vue', 1, NULL, '部门管理', 'menu', 1, '', '/api/v1/system/department/getAll:POST,/api/v1/system/department/getList:POST,/api/v1/system/department/delete:POST,/api/v1/system/department/deleteByIds:POST,/api/v1/system/department/create:POST,/api/v1/system/department/update:POST', 99999, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (25, '2021-10-24 14:26:42', '2021-10-24 21:44:06', NULL, 0, 6, 'message', 'message', 0, 'modules/system/views/message/Index.vue', 1, NULL, '消息管理', 'menu', 1, '', '/api/v1/system/message/getList:POST,/api/v1/system/message/update:POST,/api/v1/system/message/delete:POST,/api/v1/system/message/deleteByIds:POST,/api/v1/system/message/create:POST', 99999, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (26, '2021-10-24 21:36:53', '2021-10-24 21:36:53', NULL, NULL, 11, '', 'user_add', 0, '', 0, NULL, '新增', 'plus-circle', 0, '', '', 0, 0);
INSERT INTO sys_permissions (id, created_at, updated_at, deleted_at, level, parent_id, path, name, hidden, component, keep_alive, default_menu, title, icon, status, redirect, api, sort_order, is_button) VALUES (27, '2021-10-24 14:26:42', '2021-10-24 21:44:06', NULL, 0, 6, 'redis', 'redis', 0, 'modules/system/views/redis/Index.vue', 1, NULL, '缓存管理', 'cloud-server', 1, '', '/api/v1/system/redis/getList:POST', 99999, 0);`

		sqls := strings.Split(permissionSql, "\n")

		for _, sql := range sqls {
			if err := global.GormDB.Exec(sql).Error; err != nil {
				return err
			}
		}
		color.Info.Println("\n[Mysql] --> sys_permissions 表初始数据成功!")
		return nil
	})
}
